﻿<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>黑名单人员管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../layuiAdmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../layuiAdmin/style/admin.css" media="all">
</head>

<body>
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <input type="text" name="name" placeholder="请输入人员名称" autocomplete="off"
                               class="layui-input">
                    </div>
                    <div class="layui-inline">
                        <input type="text" name="ip" placeholder="请输入IP" autocomplete="off"
                               class="layui-input">
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn layuiadmin-btn-useradmin" layadmin-event="reset">
                            <i class="layui-icon layui-icon-refresh layuiadmin-button-btn"></i>
                        </button>
                        <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="form-search">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
            </div>
            <div class="layui-card-body">
                <table id="blacklist-manage" lay-filter="blacklist-manage"></table>
            </div>
        </div>
    </div>
    <script src="../../layuiAdmin/layui/layui.js"></script>
    <script>
        layui.config({
            base: '../../layuiAdmin/', //静态资源所在路径
            version: top.layui.setter.version
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index', 'form', 'table', 'util'], function () {
            var $ = layui.$,
                form = layui.form,
                table = layui.table,
                util = layui.util,
                admin = layui.admin;

            window.areaName = "blacklist-manage";
            var field = {};

            //监听搜索
            form.on('submit(form-search)', function (data) {
                field = data.field;
                //执行重载
                table.reload(areaName, {
                    where: { queryJson: JSON.stringify(field) },
                    page: {
                        curr: 1
                    }
                });
            });
            //管理
            table.render({
                elem: '#' + areaName,
                toolbar: $.getToolButtons(),
                defaultToolbar: [],
                url: '/api/Blacklist/GetBlacklistPageList',
                cols: [
                    [
                        { type: 'checkbox', fixed: 'left' },
                        { field: 'number', title: '序号', width: 80, align: 'center', type: 'numbers' },
                        { field: 'cIp', title: 'IP', align: 'center', width: 100 },
                        { field: 'cUserId', title: '用户', width: 150, templet: (r) => r.user?.cUserName||'' },
                        { field: 'cTerm', title: '封禁时间', width: 100, align: 'center', },
                        { field: 'cForbiddenStartTime', title: '封禁开始时间', width: 150, align: 'center', templet: (r) => r.cForbiddenStartTime.substring(0, 10) },
                        { field: 'cForbiddenEndTime', title: '封禁结束时间', width: 150, align: 'center', templet: (r) => r.cForbiddenEndTime.substring(0, 10) },
                        { field: 'cDescription', title: '封禁原因', minWidth: 180, align: 'center', templet: function (r) { return r.cDescription||'' } },
                        { field: 'cState', title: '是否有效', width: 120, align: 'center', templet: (r) => r.cState ? '有效' : '失效' },
                    
                        {
                            title: '操作', width: 250, align: 'center', fixed: 'right', templet: (r) => {
                                var $html = $($.getRowButtons());
                                if (!r.cState) {
                                    $html.find('[lay-active="x-invalid"]').remove();
                                }
                                return $html.prop('outerHTML');
                            }
                        }
                    ]
                ],
                page: true,
                limit: 50,
                height: 'full-110',
                text: '对不起，加载出现异常！'
            });
            $.TableSet(table, areaName);
            var selectRow = null;
            table.on('row(' + areaName + ')', function (obj) {
                selectRow = obj.data;
                obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
            });
            var edit = function () {
                $.OpenTab({
                    type: 2,
                    title: '编辑',
                    content: 'blacklistform.html?id=' + selectRow.cId + '&cUserName=' + selectRow.user.cUserName,
                    area: ['860px', '450px'],
                    btn: ['确定', '取消']
                });
            }
            var add = function () {
                $.OpenTab({
                    type: 2,
                    title: '查询用户列表',
                    content: 'usersearch.html',
                    area: ['1100px', '620px'],
                    end: function () {
                        table.reload(areaName);
                    }
                });
            }

            var del = function (type) {
                let ids = [];
                if (type == 1 || type == undefined) {
                    if (selectRow == null) return layer.msg('请选择数据');
                    ids = [selectRow.cId];
                } else if (type == 2) {
                    var checkData = table.checkStatus(areaName).data; //得到选中的数据
                    if (checkData.length === 0) return layer.msg('请选择数据');
                    ids = checkData.map(t => t.cId);
                } else {
                    return layer.msg('请选择删除类型');
                }

                layer.confirm('确定删除吗？', function (index) {
                    admin.req({
                        type: 'post',
                        url: '/api/Blacklist/DelBlacklist',
                        data: { ids: ids.join(','), },
                        success: function (data) {
                            if (data.code == 200) {
                                table.reload(areaName);
                                layer.close(index);
                                layer.msg('已删除!', { icon: 1, time: 1000 });
                            } else {
                                layer.close(index);
                                layer.msg(data.msg, { icon: 2, time: 1000 });
                            }
                        }
                    });
                });
            }
            var invalid = function () {
                if (selectRow == null) return layer.msg('请选择数据');
                layer.confirm('确认将此条数据设为失效！', {
                    btn: ['确定', '关闭'] //按钮
                }, function () {
                    admin.req({
                        type: 'post',
                        async: false,
                        url: '/api/Blacklist/SetInvalid',
                        data: { cId: selectRow.cId, },
                        success: function (data) {
                            if (data.code == 200) {
                                table.reload(areaName);
                                layer.msg('成功!', { icon: 1, time: 1000 });
                            } else {
                                layer.msg(data.msg, { icon: 2, time: 1000 });
                            }
                        }
                    });
                });
            };
            //事件
            util.event('lay-active', {
                'x-add': () => add(),
                'x-delete': () => del(),
                'x-batch-delete': () => del(2),
                'x-edit': () => edit(),
                'x-invalid': () => invalid()
            });
        });
    </script>
</body>

</html>